/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package CONTROLLER_ADMIN;
import java.util.*;
import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
/**
 *
 * @author samph
 */
public class LibararyQuestion {
    public  static  ArrayList docDanhSachCauHoi() {
        ArrayList dsch = new ArrayList();
        try {
              CauHoi ch = new CauHoi();
              File file = new File("c:\\cauhoi.xml");
              DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
              DocumentBuilder db = dbf.newDocumentBuilder();
              Document doc = db.parse(file);
              doc.getDocumentElement().normalize();
              System.out.println("Root element " + doc.getDocumentElement().getNodeName());
              NodeList nodeLst = doc.getElementsByTagName("Cauhoi");
              //System.out.println("Information of all employees");

              for (int s = 0; s < nodeLst.getLength(); s++) {

                Node fstNode = nodeLst.item(s);

                if (fstNode.getNodeType() == Node.ELEMENT_NODE) {

                      Element Elmnt = (Element) fstNode;
                      
                      
                      NodeList NmElmntLstid = Elmnt.getElementsByTagName("Idcauhoi");
                      Element fstNmElmntid = (Element) NmElmntLstid.item(0);
                      NodeList fstNmid = fstNmElmntid.getChildNodes();
                      ch.setIdcauhoi(Integer.parseInt(fstNmid.item(0).getNodeValue()));
                      
                      
                      
                      NodeList NmElmntLst = Elmnt.getElementsByTagName("Noidung");
                      Element fstNmElmnt = (Element) NmElmntLst.item(0);
                      NodeList fstNm = fstNmElmnt.getChildNodes();
                      ch.setQuestion(fstNm.item(0).getNodeValue());
                      
                      
                      
                      
                      
                      //System.out.println("First Name : "  + ((Node) fstNm.item(0)).getNodeValue());            
                      NodeList lstNmElmntLst = Elmnt.getElementsByTagName("Luachon");
                      
                      for(int i = 0 ; i<4; i++)
                      {
                          Element lstNmElmnt = (Element) lstNmElmntLst.item(i);
                          NodeList lstNm = lstNmElmnt.getChildNodes();
                          //System.out.println("Last Name : " + ((Node) lstNm.item(0)).getNodeValue());
                          switch(i){
                              case 0: ch.setAnswer_1(lstNm.item(0).getNodeValue());
                                  break;
                              case 1: ch.setAnswer_2(lstNm.item(0).getNodeValue());
                                  break;
                              case 2: ch.setAnswer_3(lstNm.item(0).getNodeValue());
                                  break;
                              case 3: ch.setAnswer_4(lstNm.item(0).getNodeValue());
                                  break;
                          }   
                      }
                      NodeList dapan = Elmnt.getElementsByTagName("Dapan");
                      Element elmdapan = (Element) dapan.item(0);
                      NodeList nldapan = elmdapan.getChildNodes();
                      //System.out.println("Dáp án : "  + ((Node) nldapan.item(0)).getNodeValue()); 
                      ch.setResult(Integer.parseInt(nldapan.item(0).getNodeValue()));

                      NodeList nstLevel = Elmnt.getElementsByTagName("Dokho");
                      Element elmlevel = (Element)nstLevel.item(0);
                      NodeList lstLevel = elmlevel.getChildNodes();
                       //System.out.println("độ khó : "  + ((Node) lstLevel.item(0)).getNodeValue()); 
                      ch.setResult(Integer.parseInt(lstLevel.item(0).getNodeValue()));
                 }
                dsch.add(ch);
              }
          } catch (Exception e) {
            e.printStackTrace();
          }
        return dsch;
    }
}
